package com.jiang.专题.哈希表.Q290;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Jiang
 * @version 1.0
 * @date 2025/01/24 20:12
 */
class Solution {
    public boolean wordPattern(String pattern, String s) {
        Map<Character, String> map1 = new HashMap<>();
        Map<String, Character> map2 = new HashMap<>();
        String[] arr = s.split(" ");
        if (pattern.length() != arr.length) return false;
        for (int i = 0; i < arr.length; i++) {
            Character c = pattern.charAt(i);
            if (map1.get(c) == null && map2.get(arr[i]) == null) {
                map1.put(c, arr[i]);
                map2.put(arr[i], c);
            } else if (!arr[i].equals(map1.get(c)) || map2.get(arr[i]) != c) {
                return false;
            }
        }
        return true;
    }
}
